﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;

namespace MyMovies
{
    internal class MkvRenamer
    {
        private static BackgroundWorker bw = new BackgroundWorker();
        private static bool firstTime = true;
        private static string status = string.Empty;

        public delegate void MessageEventHandler(MessageEventArgs e);
        public static event MessageEventHandler ScanStart;
        public static event MessageEventHandler ScanEnd;

        public static List<FileDuplicated> DuplicatedFiles = new List<FileDuplicated>();

        public static void Rename(string folder)
        {
            if (firstTime)
            {
                bw.WorkerReportsProgress = true;
                bw.WorkerSupportsCancellation = true;
                bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
                bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
                firstTime = false;
            }

            if (bw.IsBusy != true)
            {
                if (ScanStart != null)
                    ScanStart(new MessageEventArgs(Properties.Resources.ScanFolder));

                bw.RunWorkerAsync(folder);
            }
        }

        private static void StopScan()
        {
            if (bw.WorkerSupportsCancellation == true)
            {
                bw.CancelAsync();
            }
        }

        private static void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if ((e.Cancelled == true))
            {
                status = Properties.Resources.Canceled;
            }
            else if (!(e.Error == null))
            {
                status = string.Format(Properties.Resources.Error, e.Error.Message);
            }
            else
            {
                status = Properties.Resources.Completed;
            }
            if (ScanEnd != null)
                ScanEnd(new MessageEventArgs(status));
        }

        private static void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
        }

        private static void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            if ((worker.CancellationPending == true))
            {
                e.Cancel = true;
                return;
            }
            string path = e.Argument.ToString();

            foreach (string directory in Directory.GetDirectories(path))
                RenameFiles(directory);
        }

        private static void RenameFiles(string directory)
        {
            if (!directory.EndsWith(@"\Serie"))
                foreach (string file in Directory.GetFiles(directory))
                {
                    string ext = Path.GetExtension(file);
                    if (string.Compare(ext, ".mkv", true) == 0)
                    {
                        string oldName = Path.Combine(directory,file);
                        string newName = Path.Combine(directory,string.Concat(Path.GetFileNameWithoutExtension(file), ".mkv", ".avi"));
                        File.Move(oldName, newName);

                    }
                }
            foreach (string subdirectory in Directory.GetDirectories(directory))
                if (!subdirectory.StartsWith("_"))
                    RenameFiles(subdirectory);
        }
    }
}